<template>
  <div class="box">
    <div>
      <span>姓名:</span>
      <input type="text" v-model="form.name" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model="form.age" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="form.gender">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="add">{{ isEdit ? '修改' : '添加' }}</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item, i) in students" :key="i">
          <td>{{ i + 1 }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>{{ item.gender }}</td>
          <td>
            <button @click="del(i)">删除</button>
            <button @click="edit(item, i)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
    <!-- <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>是否确认删除</span>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="
            () => {
              dialogVisible = false
            }
          "
          >取 消</el-button
        >
        <el-button type="primary" @click="delStudent">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>
<script>
export default {
  name: 'ComponentName',
  data() {
    return {
      students: [],
      form: { name: '', age: 0, gender: '男' },
      dialogVisible: false,
      operateId: null,
      isEdit: false
    }
  },
  computed: {},
  methods: {
    add() {
      if (!this.form.name || !this.form.age) {
        this.$message({
          message: '内容不能为空',
          center: true,
          type: 'error'
        })
        return
      }
      if (this.isEdit) {
        this.students.splice(this.operateId, 1, { ...this.form })
        this.operateId = null
      } else {
        this.students.push({ ...this.form })
      }
      this.form = { name: '', age: 0, gender: '男' }
      this.isEdit = false
    },
    del(i) {
      this.operateId = i
      // this.dialogVisible = true
      this.students.splice(this.operateId, 1)
    },
    edit(item, i) {
      this.isEdit = true
      this.operateId = i
      this.form = { ...item }
    }
    // delStudent() {
    //   this.students.splice(this.operateId, 1)
    //   // this.dialogVisible = false
    // }
  }
}
</script>
<style lang="less" scoped></style>
